package com.tms.service.impl;

import com.tms.domain.ProductLine;
import com.tms.mapper.develop.ProductLineMapper;
import com.tms.service.ProductLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 生产线业务实现
 *
 * @author ozj
 */
@Service
public class ProductLineServiceImpl implements ProductLineService {

    @Autowired
    private ProductLineMapper productLineMapper;

    @Override
    @Cacheable(cacheNames = "lines", key = "#no", unless = "#result == null")
    public ProductLine findProductLineById(Integer no) {
        return productLineMapper.selectByPrimaryKey(no);
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "lines", key = "#result.no", unless = "#result == null || #result.no == 0")
            },
            evict = {
                    @CacheEvict(cacheNames = "lineList", allEntries = true, beforeInvocation = true)
            }
    )
    public ProductLine createProductLine(ProductLine productLine) {
        productLineMapper.insertSelective(productLine);
        return productLine;
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(cacheNames = "lines", key = "#no"),
                    @CacheEvict(cacheNames = "lineList", allEntries = true, beforeInvocation = true)
            }
    )
    public void deleteProductLineById(Integer no) {
        productLineMapper.deleteByPrimaryKey(no);
    }

    @Override
    @Caching(
            put = {
                    @CachePut(cacheNames = "lines", key = "#productLine.no")
            },
            evict = {
                    @CacheEvict(cacheNames = "lineList", allEntries = true, beforeInvocation = true)
            }
    )
    public ProductLine updateProductLineById(ProductLine productLine) {
        int result = productLineMapper.updateByPrimaryKeySelective(productLine);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }
        return productLine;
    }

    @Override
    @Cacheable(cacheNames = "lineList", key = "'alllines'", unless = "#result == null")
    public List<ProductLine> getAllProductLine() {
        return productLineMapper.selectAllProductLine();
    }
}